package com.example.wxy.bbtapp.Service;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.example.wxy.bbtapp.DeviceService.ScreenManager;
import com.example.wxy.bbtapp.Startup.AppSettings;
import com.example.wxy.bbtapp.Startup.DbService;
import com.example.wxy.bbtapp.Common.HttpHelper;
import com.example.wxy.bbtapp.Models.RetrunValue.ResultDataList;
import com.example.wxy.bbtapp.Models.RetrunValue.VerData;
import com.example.wxy.bbtapp.Models.bindinfo;
import com.example.wxy.bbtapp.myApplication;
import com.google.gson.Gson;

import org.xutils.ex.DbException;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.example.wxy.bbtapp.Common.httpCallBack;
import com.example.wxy.bbtapp.Models.ClassTime;
import com.example.wxy.bbtapp.Models.RetrunValue.LoginData;
import com.example.wxy.bbtapp.Models.RetrunValue.ResultData;
import com.example.wxy.bbtapp.Models.RetrunValue.basicUrlData;
import com.example.wxy.bbtapp.Models.RetrunValue.classData;
import com.example.wxy.bbtapp.Models.RetrunValue.schoolData;

/**
 * Created by yons on 2017/9/9.
 */

public class BindService {

    private static Gson gson = new Gson();

    public static  void getCook(final  httpCallBack<String> callBack) {
        httpservice.getInstance().login(new httpCallBack<String>("获取令牌") {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                LoginData loginData = gson.fromJson(result, LoginData.class);
                if (loginData.isIsSuccess())
                {
                    myApplication.Token ="Basic "+ loginData.getData();
                    myApplication.TokenTime = new Date();
                    callBack.onSuccess(loginData.getData());
                }

                else
                    Toast.makeText(myApplication.getContext(), loginData.getMessage(), Toast.LENGTH_LONG).show();
            }
        });
    }

    public static void SendAppPacket(Context c)
    {

        if(!ScreenManager.getScreenInfo(c) || myApplication.Token == null || myApplication.Token.length() ==0)
            return;
String CourseIndex = "0";
       if(ServiceBus.Current_CourseRecord != null)
                CourseIndex = ServiceBus.Current_CourseRecord.getCourseIndex() + "";
        new HttpHelper(AppSettings.PacketUrl , "RestApi/CCAppPacket/Insert")
                .addParameter("terminal_id", myApplication.bindinofs.getDeviceID())
                .addParameter("school_id", myApplication.bindinofs.getSchoolID())
                .addParameter("school_name", myApplication.bindinofs.getSchoolName())
                .addParameter("class_room_id", myApplication.bindinofs.getClassID())
                .addParameter("class_room_name", myApplication.bindinofs.getClassName())
                .addParameter("course_index", CourseIndex)
                .post(new httpCallBack<String>("提交心跳包"){
                    @Override
                    public void onSuccess(String result) {
                        super.onSuccess(result);
                        Log.v(AppSettings.DebugTag,"SendAppPacket");
                    }
                });
    }

    public static  void GetSchoolList(final httpCallBack<ArrayList<schoolData.DataBean>> callBack)
    {
        httpservice.getInstance().getSchoolData(myApplication.Token, new httpCallBack<String>("获取学校列表") {
            @Override
            public void onSuccess(String result) {
                final schoolData schoolData = gson.fromJson(result, schoolData.class);
                ArrayList<schoolData.DataBean> allchool = schoolData.getData();

                callBack.onSuccess(allchool);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.v(AppSettings.DebugTag,"访问学校"+ ex.getMessage());
            }
        });
    }

    public  static void GetClassList(final String SchoolID, final httpCallBack<ArrayList<classData.DataBean>> callBack)
    {

        httpservice.getInstance().getBasicUrl(SchoolID, myApplication.Token, new httpCallBack<String>("获取服务器信息"){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                basicUrlData urlData = gson.fromJson(result, basicUrlData.class);
                if(urlData.getData().size() > 0 && urlData.getData().get(0).getIp_address() != null)
                myApplication.bindinofs.setServerUrl(urlData.getData().get(0).getIp_address());

                httpservice.getInstance().getclassData(myApplication.Token, myApplication.bindinofs.getServerUrl(), SchoolID, new httpCallBack<String>("获取班级列表") {
                    @Override
                    public void onSuccess(String result) {
                        super.onSuccess(result);
                        classData classdata = gson.fromJson(result, classData.class);
                        ArrayList<classData.DataBean> data = classdata.getData();
                        if(callBack != null)
                            callBack.onSuccess(data);
                    }
                });
            }
        });


    }
    public static void GetClassTime(String SchoolID, final httpCallBack<List<ClassTime>> callBack)  {

        new HttpHelper("RestApi/CourseView/GetCourseIndex").addParameter("_IsPaging", "false")
                .addParameter("SchoolID", SchoolID)
                .post(new httpCallBack<String>("获取时间表"){
                    @Override
                    public void onSuccess(String result) {
                        super.onSuccess(result);
                        ResultDataList<ClassTime> dataType = new ResultDataList<ClassTime>();
                        List<ClassTime> data = ResultDataList.fromJson(result, ClassTime.class).getData();

                        ClassTimeService s = new ClassTimeService();
                        try
                        {
                            s.Save(data);
                        }
                        catch (DbException e)
                        {
                            Log.e("Error", e.getMessage());
                        }
                        if(callBack != null)
                            callBack.onSuccess(data);
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        super.onError(ex, isOnCallback);
                    }
                });
    }

    public  static void getVer(final httpCallBack<VerData> callBack) {

        httpservice.getInstance().getAppVersion(new httpCallBack<String>("获取更新版本") {
            @Override
            public void onSuccess(String result) {
                 ResultDataList<VerData> verData =  new ResultDataList<VerData>();
                List<VerData>  data = ResultDataList.fromJson(result,VerData.class).getData();

//                ResultData<VerData> ver = ResultData.fromJson(result, VerData.class);
                //VerData ver = gson.fromJson(result,VerData.class);
                if (callBack != null)
                    callBack.onSuccess( data.get(0));
            }

        });
    }



    public static  void SaveBindInfo(final String schoolName,final String className,final String schoolID,final String ClassRoomId, String CreateDate, final httpCallBack<Boolean> callBack) {
        //save schoolid,classid
        final Boolean isBind = true;
        myApplication.bindinofs.setSchoolID(schoolID);
        myApplication.bindinofs.setClassID(ClassRoomId);
        myApplication.bindinofs.setSchoolName(schoolName);
        myApplication.bindinofs.setClassName(className);
        UUID uuid = UUID.randomUUID();
        final String DeviceID = uuid.toString();
        httpservice.getInstance().bindEquipment(DeviceID,myApplication.Token, schoolID, myApplication.phoneInfo.getPhoneName(), myApplication.phoneInfo.getMacAddress(),
                myApplication.phoneInfo.getPhoneVersion(), myApplication.phoneInfo.getIpAdress(), ClassRoomId, CreateDate, new httpCallBack<String>("绑定设备") {
                    @Override
                    public void onSuccess(String result) {

                        ResultData<String> resultData = new ResultData<String>();
                        Boolean ok = ResultData.fromJson(result, String.class).isSuccess();
                        if (ok) {
                            myApplication.bindinofs.setDeviceID(DeviceID);
                            if(ServiceBus.Current_CourseRecord !=null)
                                ServiceBus.Current_CourseRecord.setDeviceID(DeviceID);
                            try {
                                DbService.getDB().delete(bindinfo.class);
                                DbService.getDB().saveBindingId(myApplication.bindinofs);
                            } catch (DbException e) {
                                Log.e("Error", e.getMessage());
                            }

                            if (callBack != null)
                                callBack.onSuccess(ok);
                        }

                    }
                });
    }

    public static bindinfo LoadBindInfo() {
        //save schoolid,classid
        try {
            bindinfo data = DbService.getDB().findFirst(bindinfo.class);
            return data;
        } catch (DbException e) {
            Log.e("Error", e.getMessage());
        }
        return null;
    }
}
